import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import(/* webpackChunkName: "login" */ '../components/page/Login.vue'),
  },
  {
    path: '/home',
    component: () => import( /* webpackChunkName: "home" */ '../components/common/Home.vue'),
    redirect:'/dashboard',
    beforeEnter (to, from, next) {
      store.dispatch('tryAutoLogin')
      console.log("token=>",store.state.idToken)
      if (store.state.idToken) {
        next()
      } else {
        next('/')
      }
    },
    afterEach(to, from){
      store.state.isLoginPage = false
    },
    meta: {
      title: '主页'
    },
    children: [{
      path: '/dashboard',
      component: () => import( /* webpackChunkName: "dashboard" */ '../components/page/Dashboard.vue'),
      meta: {
        title: '主页'
      }
    },{
      path: '/images',
      component: () => import( /* webpackChunkName: "images" */ '../components/page/Images.vue'),
      meta: {
        title: '图库'
      }
    },{
      path: '/info',
      component: () => import( /* webpackChunkName: "info" */ '../components/page/Info.vue'),
      meta: {
        title: '个人信息'
      }
    },{
      path: '/myMenu',
      component: () => import( /* webpackChunkName: "info" */ '../components/page/MyMenu.vue'),
      meta: {
        title: '我的菜单'
      }
    },{
      path: '/bill',
      component: () => import( /* webpackChunkName: "info" */ '../components/page/Bill.vue'),
      meta: {
        title: '我的账单'
      }
    }]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
